package com.amigo.online.provider.sync.handle.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.UserMysqlInfo;
import com.amigo.online.provider.sync.abs.BasicSolrDataProjectProperties;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;
import com.amigo.online.provider.sync.handle.data.solr.service.UserSolrService;
import com.amigo.online.provider.sync.handle.service.AbstractDataHandler;
import com.amigo.online.provider.sync.handle.service.DataHandler;

@Service("userSolrDataHandler")
public class UserSolrDataHandler extends AbstractDataHandler implements DataHandler {

    @Autowired
    UserSolrService userSolrService;

    @Autowired
    BasicSolrDataProjectProperties basicSolrDataProjectProperties;

    @Autowired
    @Qualifier("solrUserConvertServiceImpl")
    ConvertJsonObjectService<SolrInputDocument, JSONObject, UserMysqlInfo> solrUserconvertJsonObjectService;

    @Override
    public void updateHandle(JSONObject newObject, JSONObject oldObject) {
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", newObject.get("id"));
        boolean updateFlag = false;
        Map<String, String> projectMap = basicSolrDataProjectProperties.getUserProjectMap();
        Map<String, String> classMap = basicSolrDataProjectProperties.getUserClassMap();
        for (String temp : oldObject.keySet()) {
            if (basicSolrDataProjectProperties.getUserUpdateList().contains(temp)) {
                String solrColumnName = projectMap.get(temp);
                String solrUserClassName = classMap.get(temp);
                if (solrColumnName != null) {
                    temp = solrColumnName;
                }
                Map<String, Object> partialUpdate = new HashMap<>(4);
                if (solrUserClassName == null) {
                    partialUpdate.put("set", newObject.get(temp));
                } else {
                    try {
                        Class<?> clazz = Class.forName(solrUserClassName);
                        partialUpdate.put("set", newObject.getObject(temp, clazz));
                    } catch (ClassNotFoundException e) {
                        logger.info("更新获取User中" + temp + "字段类型失败！");
                    }
                }
                document.addField(temp, partialUpdate);
                updateFlag = true;
            }
        }
        if (updateFlag == true) {
            document.addField("_version_", 1);
            userSolrService.totalUpdate(document);
        }
		
		/*for(String temp : oldObject.keySet()) {
			if(SOLR_USER_UPDATE_COL.contains(temp)) {
				Map<String, Object> partialUpdate = new HashMap<>(4);
				partialUpdate.put("set", newObject.get(temp));
				document.addField(temp, newObject.get("temp"));
				updateFlag = true;
			}
		}
		document.addField("_version_", 1);
		userSolrService.partUpdate(document);*/

    }

    @Override
    public void insertHandle(JSONObject newObject) {
        SolrInputDocument document = solrUserconvertJsonObjectService.convert(newObject);
        userSolrService.insertObject(document);
    }

    @Override
    public void updateInfoByIdThroughHashMap(Long id, Map<String, Object> updateMap) {
        //  Auto-generated method stub

    }

    @Override
    public void insertObject(Object source) {
        SolrInputDocument document = solrUserconvertJsonObjectService.convertBeanToObject((UserMysqlInfo) source);
        userSolrService.insertObject(document);
    }

}
